/*
 *  Copyright 2011 Alexey Andreev.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.xthl.core;

import java.util.ArrayList;
import java.util.List;
import org.nop.core.AbstractController;
import org.nop.core.Content;
import org.nop.core.RouteBinding;
import org.nop.util.Injected;
import org.xthl.core.api.Role;
import org.xthl.core.api.RoleLogic;
import org.xthl.core.api.RoleTypeFilter;
import org.xthl.core.api.UpdateRoleCommand;
import org.xthl.core.api.UserLogic;
import org.xthl.core.view.DefaultRolesForm;
import org.xthl.core.view.DefaultRolesView;
import org.xthl.core.view.RoleEditForm;
import org.xthl.core.view.RoleEditView;
import org.xthl.core.view.RoleListView;
import org.xthl.core.view.RoleView;

/**
 *
 * @author Alexey Andreev
 */
@RouteBinding(RolesRoute.class)
public class RolesController extends AbstractController {
    private RoleLogic logic;
    private UserLogic userLogic;
    
    @Injected
    public RolesController(RoleLogic logic, UserLogic userLogic) {
        this.logic = logic;
        this.userLogic = userLogic;
    }

    public Content list() {
        userLogic.requirePermission("user.manage");
        List<Role> roles = logic.getRoles(RoleTypeFilter.ALL);
        return html(createView(RoleListView.class).setRoles(roles));
    }
    
    public Content view(int roleId) {
        userLogic.requirePermission("user.manage");
        Role role = logic.getRole(roleId);
        List<String> permissions = logic.getRolePermissions(roleId);
        return html(createView(RoleView.class).setRole(role)
                .setPermissions(permissions));
    }
    
    public Content edit(RoleEditForm form, int roleId) {
        userLogic.requirePermission("user.manage");
        Role role = logic.getRole(roleId);
        if (role == null) {
            return null;
        }
        if (!form.isPosted()) {
            form.setName(role.getName());
            form.setSystem(role.isSystem());
            form.getPermissions().addAll(logic.getRolePermissions(roleId));
        }
        List<String> permissions = logic.getPermissions();
        return html(createView(RoleEditView.class)
                .setRole(role)
                .setAllPermissions(permissions)
                .setForm(form));
    }
    
    public Content save(RoleEditForm form, int roleId) {
        UpdateRoleCommand cmd = new UpdateRoleCommand();
        cmd.setSystem(form.isSystem());
        cmd.setName(form.getName());
        logic.setRole(roleId, cmd);
        logic.setRolePermissions(roleId, new ArrayList<String>(form.getPermissions()));
        return view(roleId);
    }
    
    public Content add(RoleEditForm form) {
        return html(createView(RoleEditView.class)
                .setAllPermissions(logic.getPermissions())
                .setForm(form)
                .setRole(null));
    }
    
    public Content save(RoleEditForm form) {
        UpdateRoleCommand cmd = new UpdateRoleCommand();
        cmd.setSystem(form.isSystem());
        cmd.setName(form.getName());
        Role role = logic.createRole(cmd);
        logic.setRolePermissions(role.getId(), new ArrayList<String>(form.getPermissions()));
        return redirect(route(RolesRoute.class).view(role.getId()));
    }
    
    public Content editDefault(DefaultRolesForm form) {
        form.getDefaultRoleIds().clear();
        form.getDefaultRoleIds().addAll(logic.getDefaultRoleIds());
        form.getGuestRoleIds().clear();
        form.getGuestRoleIds().addAll(logic.getGuestRoleIds());
        return html(createView(DefaultRolesView.class)
                .setForm(form)
                .setRoles(logic.getRoles(RoleTypeFilter.ALL)));
    }
    
    public Content save(DefaultRolesForm form) {
        logic.setDefaultRoleIds(form.getDefaultRoleIds());
        logic.setGuestRoleIds(form.getGuestRoleIds());
        return editDefault(form);
    }
}
